import { AbstractDao } from './AbstractDao';
/*
  * Copyright (c) 2022 Huawei Device Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
    *
  * http://www.apache.org/licenses/LICENSE-2.0
    *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

import { AbstractDaoMaster } from './AbstractDaoMaster';
import { StandardDatabase } from './database/StandardDatabase';
import { Database } from './database/Database';
import { DatabaseOpenHelper } from './database/DatabaseOpenHelper';
import { IdentityScopeType } from './identityscope/IdentityScopeType';
import { BaseDao } from './BaseDao';
import { DaoSession } from './DaoSession';

var schema_version: number = 1;
// THIS CODE IS GENERATED BY dataORM, DO NOT EDIT.
/**
 * Master of DAO (schema version 1000): knows all DAOs.
 */
export class DaoMaster extends AbstractDaoMaster {

    /** Creates underlying database table using DAOs. */
    public static createAllTables(db: Database, ifNotExists: boolean): void {
        let entities = globalThis.entityClsArr;
        if (entities) {
            for (let entity of entities) {
                BaseDao.createTable(db, ifNotExists, entity);
            }
        }
        AbstractDao.allTableIsCreateEnd()
    }

    /** Drops underlying database table using DAOs. */
    public static dropAllTables(db: Database, ifExists: boolean): void {
        let entities = globalThis.entityClsArr;
        if (entities) {
            for (let entity of entities) {
                BaseDao.dropTable(db, ifExists, entity);
            }
        }
    }

    /**
     * WARNING: Drops all table on Upgrade! Use only during development.
     * Convenience method using a {@link DevOpenHelper}.
     */
    public static async newDevSession(context: any, name: string): Promise<DaoSession> {
        let db: Database = await new DevOpenHelper(context, name).getWritableDb();
        let daoMaster: DaoMaster = new DaoMaster(db);
        let daoSession: DaoSession = daoMaster.newSession()
        return daoSession;
    }

    constructor(db: any) {
        let cls = db.constructor.name;
        if (cls == 'StandardDatabase') {
            super(db, schema_version);
        } else {
            super(new StandardDatabase(db), schema_version);
        }
        let entities = globalThis.entityClsArr;
        if (entities) {
            for (let entity of entities) {
                this.registerDaoClass(entity);
            }
        }
    }

    public newSession(type_s=IdentityScopeType.Session): DaoSession {
        return new DaoSession(this.db, type_s, this.daoConfigMap);
    }
}


/**
 * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
 */
export abstract class OpenHelper extends DatabaseOpenHelper {
    constructor(context: any, name: string, factory?: any) {
        super(context, name, schema_version, factory);
    }

    public onCreate_D(db: Database): void {
        DaoMaster.createAllTables(db, true);
    }
}

/** WARNING: Drops all table on Upgrade! Use only during development. */
export class DevOpenHelper extends OpenHelper {
    constructor(context: any, name: string, factory?: any) {
        super(context, name, factory);

    }

    public onUpgrade_D(db: Database, oldVersion: number, newVersion: number): void {
        let migrations = this.getMigration();
        if (migrations) {
            migrations.forEach(migrations_item => {
                migrations_item.execute(globalThis.contt,this.encrypt)
            });
        }
    }
}
